package emulator.hardware;

public class HwNumber implements Comparable<HwNumber> {
	long number = 0;

	public HwNumber(int number) {
		setNumber(number);
	}

	public HwNumber(long number) {
		setNumber(number);
	}

	public void setNumber(long number) { this.number = number; normalize(); }
	public long getNumber() { return number; }
	
	void normalize() {}
	
	public void add(HwNumber op2)
	{
		number += op2.getNumber();
		normalize();
	}
	
	public void sub(HwNumber op2)
	{
		number -= op2.getNumber();
		normalize();
	}
	
	public void mul(HwNumber op2)
	{
		number *= op2.getNumber();
		normalize();
	}
	
	public void div(HwNumber op2)
	{
		number /= op2.getNumber();
		normalize();
	}
	
	public void not()
	{
		number = ~number;
		normalize();
	}
	
	public void and(HwNumber op2)
	{
		number &= op2.getNumber();
		normalize();
	}
	
	public void or(HwNumber op2)
	{
		number |= op2.getNumber();
		normalize();
	}
	
	public void xor(HwNumber op2)
	{
		number ^= op2.getNumber();
		normalize();
	}
	
	public void shift(int count)
	{
		if (count > 0)
			number <<= count;
		else if (count < 0)
			number >>= -count;
		normalize();
	}
	
	public boolean isGreaterThan(HwNumber op2)
	{
		return number > op2.getNumber();
	}

	public boolean isEqualTo(HwNumber op2)
	{
		return number == op2.getNumber();
	}
	
	public boolean isLessThan(HwNumber op2)
	{
		return number < op2.getNumber();
	}
	
	public void inc(int i) {
		number += i;
		normalize();
	}

	public static HwNumber create(int number)
	{
		HwNumber result = new HwNumber(number);
		return result;
	}
	
	public void setBit(int bit, boolean val) {
		int mask = 1 << bit;
		if (val)	number |= mask;
		else		number &= ~mask;
		normalize();
	}

	public boolean getBit(int bit) {
		int mask = 1 << bit;
		return (number & mask) != 0;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return Long.valueOf(getNumber()).hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof HwNumber)
			return getNumber() == ((HwNumber)obj).getNumber();
		if (obj instanceof Number)
			return getNumber() == ((Number)obj).longValue();
		return super.equals(obj);
	}

	@Override
	public int compareTo(HwNumber obj) {
		return Long.signum((getNumber() - ((HwNumber)obj).getNumber()));
	}
}
